from template import *
from collect import collect

import sys

dt = 0.5e-12
timestep_output = 500000.0

me = 9.109382616e-31
mi = 4.0 * me
#mi = 2.0 * 1.67262158e-27
mr = me * mi / ( me + mi )
print("mi: ", mi)

Tx0 = 65.0
Ty0 = 50.0
detaT0 = Tx0 - Ty0


T = 55.0

ne = 1.0e19
ni = 1.0e19

charge1 = -1.0 * const.e
charge2 = 1.0 * const.e
gamma1 = 4.0 * const.pi * const.epsilon_0 * mr / abs(charge1 * charge2)
g12_square = 3.0 * T * const.e / me + 3.0 * T * const.e / mi
debye_length_e = math.sqrt(const.epsilon_0 * T / (ne * const.e))
log_coulomb = math.log( gamma1 * g12_square * debye_length_e )



#reference time, page 4647 in the ref: Theory of cumulative small-angle collisions in plasmas, K. Nanbu
t_ref = 8.0 * const.pi * math.pow(2.0, 0.5) * math.pow(const.epsilon_0, 2.0) * math.pow(me, 0.5) * math.pow(const.e * T, 1.5) / (ne * pow(const.e, 4.0) * log_coulomb)
print("log_coulomb: ", log_coulomb)
print("t_ref: ", t_ref)
print("time step: ", t_ref/dt)




##inite the fig of matplotlib
fig=plt.figure(figsize=(10,10))
fig.subplots_adjust(top=0.9,bottom=0.1,wspace=0.5,hspace=0.35)

##============ Temperature ======================================================
ax=fig.add_subplot(1,1,1)

val = collect("/Fields/", "Tx_global_e_avg")
val_1d = val[:, 0, 0, 2]
val_1d[0] = Tx0
print("Tx: ", val_1d)

nt = (val.shape)[0]
t = np.arange(0, nt) * dt * timestep_output / t_ref

line0=ax.plot(t, val_1d/T, label = "PIC", color = "blue", linestyle = linestyles[0])


val = collect("/Fields/", "Ty_global_e_avg")
val_1d = val[:, 0, 0, 2]
val_1d[0] = Ty0
print("Ty: ", val_1d)

line0=ax.plot(t, val_1d/T, color = "blue", linestyle = linestyles[0])


detaT = detaT0 * np.exp(-8.0 * t / (5.0 * math.pow(2.0 * const.pi, 0.5)))
Tx_theory = T - (1.0 / 3.0) * detaT
Ty_theory = T + (2.0 / 3.0) * detaT

line0=ax.plot(t, Tx_theory/T, label = "theory", color = "red", linestyle = linestyles[1])
line0=ax.plot(t, Ty_theory/T, color = "red", linestyle = linestyles[1])


ax.legend(framealpha = 0.2)

ax.set_xlabel("t", fontsize = label_fontsize)
ax.set_ylabel("T (eV)", fontsize = label_fontsize)


fig_file_name = "temperature_evalution.png"
fig.savefig(fig_file_name, dpi = 300)
##fig.show()       #when the program finishes,the figure disappears
#plt.axis('equal')
#plt.show()         #The command is OK
